import paho.mqtt.client as mqtt
import json
import pymysql
import time

load = {}


def gettime():
    time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    return time1


# 服务器地址
host = 'broker.emqx.io'
# 通信端口 默认端口1883
port = 1883

# username = 'username '
# password = 'password '

# 订阅主题名
topic = 'test_topic'


# 连接后事件
def on_connect(client, userdata, flags, respons_code):
    if respons_code == 0:
        # 连接成功
        print('Connection Succeed!')
    else:
        # 连接失败并显示错误代码
        print('Connect Error status {0}'.format(respons_code))
    # 订阅信息
    client.subscribe(topic)


# 接收到数据后事件
def on_message(client, userdata, msg):
    打印订阅消息主题
    print("topic", msg.topic)

    # 补充内容
    # load = jsondata["msg"]
    # a = load["a"]
    # b = load["b"]
    # 打印消息数据
    jsondata = json.loads(msg.payload)
    print(msg.payload)
    print("msg payload", jsondata)
    # print(a, b)

    if __name__ == '__main__':
        main()

